package com.rizzo.infrastructure.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class CompoundValidator implements Validator {

    private final Validator[] validators;

    public CompoundValidator(final Validator[] validators) {
        super();
        this.validators = validators;
    }

    /**
     * Will return true if this class is in the specified map.
     */
    public boolean supports(final Class clazz) {
        for (Validator v : validators) {
            if (v.supports(clazz)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Validate the specified object using the validator registered for the object's class.
     */
    public void validate(final Object obj, final Errors errors) {
        for (Validator v : validators) {
            if (v.supports(obj.getClass())) {
                v.validate(obj, errors);
            }
        }
    }
}
